package ext.acae2.dms.parse.wc9;

import org.dom4j.Document;
import org.dom4j.Node;

import ext.acae2.dms.bean.PartUsageLinkBean;
import ext.acae2.util.LoadingXmlUtil;

public class ParseWTPartUsageLink {

	public static PartUsageLinkBean execute(String path) throws Exception{
		PartUsageLinkBean partUsageLinkBean = new PartUsageLinkBean();
		Document doc = LoadingXmlUtil.executeForDom4j(path);
		
		Node usedByNode = doc.selectSingleNode("/WTPartUsageLink/usedBy/ObjectReference/ufid");
		String usedBy = usedByNode.getText();
		String parentId = usedBy.substring(usedBy.lastIndexOf(":") + 1, usedBy.substring(0,usedBy.lastIndexOf("|")).lastIndexOf("|"));
		partUsageLinkBean.setParentId(parentId);
		
		Node usesNode = doc.selectSingleNode("/WTPartUsageLink/uses/ObjectReference/ufid");
		String uses = usesNode.getText();
		String childId = uses.substring(uses.lastIndexOf(":") + 1, uses.substring(0,uses.lastIndexOf("|")).lastIndexOf("|"));
		partUsageLinkBean.setChildId(childId);
		
		Node quantityAmountNode = doc.selectSingleNode("/WTPartUsageLink/quantityAmount");
		String quantityAmount = quantityAmountNode.getText();
		double singleAmout = Double.parseDouble(quantityAmount);
		partUsageLinkBean.setSingleAmout(singleAmout);
		
		return partUsageLinkBean;
	}
	
}
